import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.tangcl.module_commom.R
import java.lang.Exception

/**
 * Created by TCL on 2021/5/18.
 * Describe:BottomSheetDialogFragment基类
 */
abstract class BaseBottomSheetDialog : BottomSheetDialogFragment() {
    private var mBottomSheet: FrameLayout? = null
    private var mBehavior: BottomSheetBehavior<FrameLayout>? = null

    override fun onStart() {
        super.onStart()
        val dialog: BottomSheetDialog = dialog as BottomSheetDialog
        dialog.setCanceledOnTouchOutside(setCancelOnTouchOutside())
        //解决打开弹窗时状态栏变黑
        val window: Window? = dialog.window
        if (window != null) {
            window.statusBarColor = Color.TRANSPARENT
//            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//                window.statusBarColor = Color.TRANSPARENT
//            }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//            }
        }
        mBottomSheet = dialog.delegate.findViewById(R.id.design_bottom_sheet)
        if (mBottomSheet != null) {
            if (!isHeightSelfFit()) {
                val layoutParams: CoordinatorLayout.LayoutParams =
                    mBottomSheet!!.layoutParams as CoordinatorLayout.LayoutParams
                layoutParams.height = getHeight()
                mBottomSheet?.layoutParams = layoutParams
            }
            mBehavior = BottomSheetBehavior.from(mBottomSheet!!)
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化数据
        initData(arguments)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog)
        isCancelable = setCancelOnBackKey()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getLayoutId(), container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        setListener()
    }

    /**
     * 自定义时添加layout
     */
    abstract fun getLayoutId(): Int

    /**
     * 加载数据
     *
     * @param bundle 用这个Bundle对象接收传入时的Bundle对象
     */
    protected abstract fun initData(bundle: Bundle?)

    /**
     * 初始化
     */
    abstract fun initView(view: View)

    /**
     * 监听
     */
    protected open fun setListener() {
    }

    /**
     * 设置空白区域
     */
    protected open fun setCancelOnTouchOutside(): Boolean {
        return true
    }

    /**
     * 设置返回键
     */
    protected open fun setCancelOnBackKey(): Boolean {
        return true
    }

    /**
     * 高度是否自适应
     * （非自适应重写getHeight）
     */
    protected open fun isHeightSelfFit(): Boolean {
        return true
    }

    /**
     * 设置高度
     */
    protected open fun getHeight(): Int {
        return resources.displayMetrics.heightPixels
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            //在每个add事务前增加一个remove事务，防止连续的add
            manager.beginTransaction().remove(this).commit()
            super.show(manager, tag)
        } catch (e: Exception) {
            //同一实例使用不同的tag会异常,这里捕获一下
            e.printStackTrace()
        }
    }

}